//
//  SwiftMessageManager.swift
//  GoCash
//
//  Created by jie on 2024/5/29.
//

import SwiftUI
import SwiftMessages



class SwiftMessageManager {
    @MainActor class func showBottom(content: any View) {
        let contentName = String(describing: type(of: content))
        
        let messageHostingView = MessageHostingView(id: contentName, content: AnyView(content))
        
        var config = SwiftMessages.Config()
        config.presentationStyle = .bottom
        config.dimMode = .color(color: .black.alpha(0.2), interactive: true)
        config.duration = .forever
        config.interactiveHide = false
        config.haptic = .success
        SwiftMessages.show(config: config, view: messageHostingView)
    }
    
    @MainActor class func hidden() {
        SwiftMessages.hide()
    }
}

#Preview {
    DemoView()
}

struct DemoView: View {
    var body: some View {
        Button("Show message") {
//            let message = DemoMessage(title: "Demo", body: "SwiftUI forever!")
//            let m = MessageHostingView(id: message.id, content: DemoMessageView(message: message))
//            var config = SwiftMessages.Config()
//            config.presentationStyle = .bottom
//            config.dimMode = .gray(interactive: false)
//            config.duration = .forever
//            config.haptic = SwiftMessages.Haptic.warning
//            config.interactiveHide = true
//            SwiftMessages.show(config: config, view: m)
            SwiftMessageManager.showBottom(content: ApproveOptionsAlert(options: [
                OptionModel(value: 1, isSel: true),
                OptionModel(value: 2),
                OptionModel(value: 3),
                OptionModel(value: 4),
                OptionModel(value: 5),
                OptionModel(value: 6),
            ]){ option in
                
            }
            )
        }
        
    }
}

struct DemoMessage: Identifiable {
    
    let title: String
    let body: String

    var id: String { title + body }
}

struct DemoMessageView: View {

    let message: DemoMessage

    var body: some View {
        VStack(alignment: .leading) {
            Text(message.title).font(.system(size: 20, weight: .bold))
            Text(message.body)
        }
        
        .multilineTextAlignment(.leading)
        .padding(30)
        // This makes the message width greedy
        .frame(maxWidth: .infinity)
        .background(.gray)
        // This makes a tab-style view where the bottom corners are rounded and
        // the view's background extends to the top edge.
//        .mask(
//            UnevenRoundedRectangle(bottomLeadingRadius: 15, bottomTrailingRadius: 15)
//            // This causes the background to extend into the safe area to the screen edge.
//            .edgesIgnoringSafeArea(.top)
//        )
    }
}
